//
//  NSString+SQLChain.m
//  SQL演练
//
//  Created by GR on 2017/10/29.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "NSString+SQLChain.h"

@implementation NSString (SQLChain)

- (NSString *)integer
{
    return [self stringByAppendingString:@" INTEGER"];
}

- (NSString *)text
{
    return [self stringByAppendingString:@" TEXT"];
}

- (NSString *)real
{
    return [self stringByAppendingString:@" REAL"];
}

- (NSString *)blob
{
    return [self stringByAppendingString:@" BLOB"];
}


- (NSString *)notnull
{
    return [self stringByAppendingString:@" NOT NULL"];
}

- (NSString *)unique
{
    return [self stringByAppendingString:@" UNIQUE"];
}

- (NSStringParamBlock)defaultValue
{
    return ^ NSString* (id value) {
        if ([value isKindOfClass:[NSString class]]) {
            value = strValue(value);
        }
        return [self stringByAppendingFormat:@" DEFAULT %@", value];
    };
}

- (NSString *)primary_key
{
    return [self stringByAppendingString:@" PRIMARY KEY"];
}

- (NSString *)autoincrement
{
    return [self stringByAppendingString:@" AUTOINCREMENT"];
}

- (NSStringStringParamBlock)dot
{
    return ^ NSString *(NSString *str){
        return [self stringByAppendingFormat:@", %@", str];
    };
}

- (NSString *)as
{
    return [self stringByAppendingString:@" as "];
}

- (NSStringStringParamBlock)append
{
    return ^ NSString *(NSString *str) {
        return [self stringByAppendingString:str];
    };
}

- (NSStringParamBlock)eq
{
    return ^ NSString *(id vaule) {
        if ([vaule isKindOfClass:[NSString class]]) {
            vaule = [NSString stringWithFormat:@"'%@'", vaule];
        }
        return [self stringByAppendingFormat:@" = %@", vaule];
    };
}

- (NSStringParamBlock)neq
{
    return ^ NSString *(id vaule) {
        if ([vaule isKindOfClass:[NSString class]]) {
            vaule = [NSString stringWithFormat:@"'%@'", vaule];
        }
        return [self stringByAppendingFormat:@" != %@", vaule];
    };
}

- (NSStringParamBlock)greater
{
    return ^ NSString *(id vaule) {
        if ([vaule isKindOfClass:[NSString class]]) {
            vaule = [NSString stringWithFormat:@"'%@'", vaule];
        }
        return [self stringByAppendingFormat:@" > %@", vaule];
    };
}

- (NSStringParamBlock)greaterEq
{
    return ^ NSString *(id vaule) {
        if ([vaule isKindOfClass:[NSString class]]) {
            vaule = [NSString stringWithFormat:@"'%@'", vaule];
        }
        return [self stringByAppendingFormat:@" >= %@", vaule];
    };
}

- (NSStringParamBlock)less
{
    return ^ NSString *(id vaule) {
        if ([vaule isKindOfClass:[NSString class]]) {
            vaule = [NSString stringWithFormat:@"'%@'", vaule];
        }
        return [self stringByAppendingFormat:@" < %@", vaule];
    };
}

- (NSStringParamBlock)lessEq
{
    return ^ NSString *(id vaule) {
        if ([vaule isKindOfClass:[NSString class]]) {
            vaule = [NSString stringWithFormat:@"'%@'", vaule];
        }
        return [self stringByAppendingFormat:@" <= %@", vaule];
    };
}

- (NSStringStringParamBlock)AND
{
    return ^ NSString *(NSString *vaule) {
        return [self stringByAppendingFormat:@" AND %@",vaule];
    };
}

- (NSStringStringParamBlock)OR
{
    return ^ NSString *(NSString *vaule) {
        return [self stringByAppendingFormat:@" OR %@",vaule];
    };
}

- (NSStringStringParamBlock)like
{
    return ^NSString *(NSString *str) {
        return [self stringByAppendingFormat:@" LIKE '%%%@%%'", str];
    };
}

- (NSString *)asc
{
    return [self stringByAppendingString:@" ASC"];
}

- (NSString *)desc
{
    return [self stringByAppendingString:@" DESC"];
}

- (NSString *)count
{
    return [NSString stringWithFormat:@"count(%@)", self];
}

- (NSString *)avg
{
    return [NSString stringWithFormat:@"avg(%@)", self];
}

- (NSString *)sum
{
    return [NSString stringWithFormat:@"sum(%@)", self];
}

- (NSString *)max
{
    return [NSString stringWithFormat:@"max(%@)", self];
}

- (NSString *)min
{
    return [NSString stringWithFormat:@"min(%@)", self];
}

@end
